package com.robot.orders.service;

import com.robot.orders.dao.ConfirmationDao;
import com.robot.orders.pojo.Confirmation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * confirmation服务层
 *
 * @author lin
 */
@Service
public class ConfirmationService {
    @Autowired
    private ConfirmationDao confirmationDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Confirmation> findAll() {
        return confirmationDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Confirmation> findSearch(Map whereMap, int page, int size) {
        Specification<Confirmation> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return confirmationDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Confirmation> findSearch(Map whereMap) {
        Specification<Confirmation> specification = createSpecification(whereMap);
        return confirmationDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Confirmation findById(String id) {
        return confirmationDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param confirmation
     */
    public void add(Confirmation confirmation) {
        //雪花分布式ID生成器
        confirmation.setId(idWorker.nextId() + "");
        LocalDateTime now = LocalDateTime.now();
        confirmation.setCreatedAt(now);
        confirmation.setUpdatedAt(now);
        confirmation.setStatus("0");
        confirmationDao.save(confirmation);
    }

    /**
     * 修改
     *
     * @param confirmation
     */
    public void update(Confirmation confirmation) {
        confirmation.setUpdatedAt(LocalDateTime.now());
        confirmationDao.save(confirmation);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        confirmationDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Confirmation> createSpecification(Map searchMap) {
        return new Specification<Confirmation>() {
            @Override
            public Predicate toPredicate(Root<Confirmation> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 采购单id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 公司id
                String companyId = "companyId";
                if (searchMap.get(companyId) != null && !"".equals(searchMap.get(companyId))) {
                    predicateList.add(cb.like(root.get(companyId).as(String.class), "%" + (String) searchMap.get(companyId) + "%"));
                }
                // 店铺id
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 店铺名字
                String storeName = "storeName";
                if (searchMap.get(storeName) != null && !"".equals(searchMap.get(storeName))) {
                    predicateList.add(cb.like(root.get(storeName).as(String.class), "%" + (String) searchMap.get(storeName) + "%"));
                }
                // 代理商id
                String agentId = "agentId";
                if (searchMap.get(agentId) != null && !"".equals(searchMap.get(agentId))) {
                    predicateList.add(cb.like(root.get(agentId).as(String.class), "%" + (String) searchMap.get(agentId) + "%"));
                }
                // 代理商姓名
                String agentName = "agentName";
                if (searchMap.get(agentName) != null && !"".equals(searchMap.get(agentName))) {
                    predicateList.add(cb.like(root.get(agentName).as(String.class), "%" + (String) searchMap.get(agentName) + "%"));
                }
                // 代理商手机号
                String agentPhone = "agentPhone";
                if (searchMap.get(agentPhone) != null && !"".equals(searchMap.get(agentPhone))) {
                    predicateList.add(cb.like(root.get(agentPhone).as(String.class), "%" + (String) searchMap.get(agentPhone) + "%"));
                }
                // 区县
                String area = "area";
                if (searchMap.get(area) != null && !"".equals(searchMap.get(area))) {
                    predicateList.add(cb.like(root.get(area).as(String.class), "%" + (String) searchMap.get(area) + "%"));
                }
                // 凭证图片地址
                String voucherImage = "voucherImage";
                if (searchMap.get(voucherImage) != null && !"".equals(searchMap.get(voucherImage))) {
                    predicateList.add(cb.like(root.get(voucherImage).as(String.class), "%" + (String) searchMap.get(voucherImage) + "%"));
                }
                // 备注
                String remark = "remark";
                if (searchMap.get(remark) != null && !"".equals(searchMap.get(remark))) {
                    predicateList.add(cb.like(root.get(remark).as(String.class), "%" + (String) searchMap.get(remark) + "%"));
                }
                // 0-待确认 1-已确认 2-驳回
                String status = "status";
                if (searchMap.get(status) != null && !"".equals(searchMap.get(status))) {
                    predicateList.add(cb.like(root.get(status).as(String.class), "%" + (String) searchMap.get(status) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 根据agentId查询采购付款凭证列表
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    public Page<Confirmation> findAllByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(String agentId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return confirmationDao.findAllByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, pageable);
    }

    /**
     * 根据agentId和status查询采购付款凭证列表
     *
     * @param agentId
     * @param status
     * @param page
     * @param size
     * @return
     */
    public Page<Confirmation> findAllByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(String agentId, String status, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return confirmationDao.findAllByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, status, pageable);
    }

}
